/* 
 * File:   merge.c
 * Author: 16
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "arbreCanonique.h"
#include "listearbre.h"
#include "merge.h"
#include "stringutils.h"

void printListe2(liste* list) {
    if (list != NULL) {
        printf("%s - %d\n", list->info->lettre, list->info->val);
        printListe2(list->suiv);
    }
}

int auMoins2Elts(liste* list) {
    return list != NULL && list->suiv != NULL;
}

liste* package(liste* list, int nombreLettres) {
    int k = 0;
    liste* renvoie = NULL;
    while (k < nombreLettres - 2 && auMoins2Elts(list) != 0) {
        arbre* newElt = malloc(sizeof (arbre));
        newElt->lettre = concat(list->info->lettre, list->suiv->info->lettre);
        newElt->val = list->info->val + list->suiv->info->val;
        insererEnTete(&renvoie, newElt);
        k++;
        list = list->suiv->suiv;
    }
    return renvoie;
}

liste* merge(liste* list, liste* newElements) {
    while (newElements != NULL) {
        insertionListe(&list, newElements->info);
        newElements = newElements->suiv;
    }
    return list;
}

int nombreOccurencesString(char* string, char lettre) {
    int occurences = 0;
    while (string != NULL) {
        string = strchr(string, lettre);
        if (string != NULL) {
            occurences++;
            string++;
        }
    }
    printf("fin_string\n");

    return occurences;
}

int nombreOccurencesListe(liste* list, char lettre) {
    if (list == NULL) {
        return 0;
    } else {
        printf("pas null\n");
        return nombreOccurencesString(list->info->lettre, lettre) + nombreOccurencesListe(list->suiv, lettre);
    }
}

void calculOccurences(liste* list, int* occtab) {
    while (list != NULL) {
        int i = 0;
        //printf("%s \n", list->info->lettre);

        while (list->info->lettre[i] != '\0')
            occtab[(unsigned char) list->info->lettre[i++]]++;

        list = list->suiv;
    }
}

tableau* packageMerge(liste* original, int hauteurMax) {
    liste* listePackage = cloneListe(original);
    liste* oldPackage = NULL;
    int nombreLettres = longList(original);
    int i = 0;
    printf("%d\n", listeCmp(listePackage, oldPackage));
    while (i < hauteurMax - 1 && !listeCmp(listePackage, oldPackage)) {
        printf("while\n");
        liste* newElements = package(listePackage, nombreLettres);
        freelist(oldPackage);
        oldPackage = listePackage;
        listePackage = merge(cloneListe(original), newElements);
        i++;
    }


    tableau* tmerge = malloc(sizeof (tableau));
    tmerge->tab = (LetVal*) calloc(TAILLE_DICTIONNAIRE, sizeof (LetVal));

    int occtab[TAILLE_DICTIONNAIRE] = {0};

    calculOccurences(listePackage, occtab);

    for (int i = 0; i < 256; i++)
        printf("%c %d\n", i, occtab[i]);

    liste* it = original;
    while (it != NULL) {
        int occurence = occtab[(unsigned char) it->info->lettre[0]];
        //printf("%d\n", occtab[(unsigned int)lettre]);
        //printf("av inserer tab\n");
        insererTab(tmerge, occurence, it->info->lettre);

        it = it->suiv;
    }
    return tmerge;
}